package com.chenfan.magic.config;

import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.dialect.IDialect;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.dialect.SpringStandardDialect;
import org.thymeleaf.templatemode.TemplateMode;
import org.thymeleaf.templateresolver.StringTemplateResolver;

import java.util.Properties;

/**
 * 解析器配置类
 *
 * @author 周夕
 * @date 2020-02-26 00:47
 */
@Configuration
public class ResolverConfiguration {

    @Bean(name = "velocityResolverEngine")
    public VelocityEngine velocityResolverEngine() {
        VelocityEngine velocityEngine = new VelocityEngine();
        //设置velocity资源加载器
        Properties prop = new Properties();
        prop.setProperty("resource.loader", "classpath");
        prop.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        prop.put("output.encoding", "UTF-8");
        prop.put("input.encoding", "UTF-8");
        velocityEngine.init(prop);
        return velocityEngine;
    }


    @Bean(name = "thymeleafResolverEngine")
    public SpringTemplateEngine thymeleafResolverEngine() {
        //字符串模板引擎对象
        SpringTemplateEngine springTemplateEngine = new SpringTemplateEngine();

        //内置方言
        IDialect springStandardDialect = new SpringStandardDialect();
        springTemplateEngine.setDialect(springStandardDialect);

        //字符串解析器
        StringTemplateResolver stringTemplateResolver = new StringTemplateResolver();

        //使用缓存
        stringTemplateResolver.setCacheable(true);
        stringTemplateResolver.setTemplateMode(TemplateMode.HTML);
        springTemplateEngine.setTemplateResolver(stringTemplateResolver);
        return springTemplateEngine;
    }

}
